<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        :offset="50"
      >
        <ArticleItem
          v-for="obj in list"
          :key="obj.art_id"
          :obj="obj"
          :isShow="true"
          @disLikeEV = "disLikeFn"
          @reportEV = "reportFn"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '@/views/Home/components/ArticleItem'
import { articlesListAPI, dislikeNew, reportArticleAPI } from '@/api'
// import { Notify } from 'vant'
import Notify from '@/ui/Notify'
export default {
  data () {
    return {
      list: [],
      finished: false,
      loading: false,
      theTime: new Date().getTime(),
      isLoading: false
    }
  },
  components: {
    ArticleItem
  },
  props: ['id'],
  async created () {
    // const res = await articlesListAPI({
    //   channel_id: this.id,
    //   timestamp: this.theTime
    // })
    // this.list = [...this.list, ...res.data.data.results]
    // this.theTime = res.data.data.pre_timestamp
    this.sendRequest()
  },
  methods: {
    async sendRequest () {
      const res = await articlesListAPI({
        channel_id: this.id,
        timestamp: this.theTime
      })
      this.list = [...this.list, ...res.data.data.results]
      this.theTime = res.data.data.pre_timestamp
      this.loading = false
      if (res.data.data.results.pre_timestamp === null) {
        this.finished = true
      }
      this.loading = false
      if (res.data.data.results.pre_timestamp === null) {
        this.finished = true
      }
      this.isLoading = false
    },
    async onLoad () {
      // const res = await articlesListAPI({
      //   channel_id: this.id,
      //   timestamp: this.theTime
      // })

      // this.list = [...this.list, ...res.data.data.results]
      // this.theTime = res.data.data.pre_timestamp
      // this.loading = false
      // if (res.data.data.results.pre_timestamp === null) {
      //   this.finished = true
      // }
      if (this.list.length !== 0) { this.sendRequest() }
    },
    async onRefresh () {
      this.list = []
      this.pre_timestamp = (new Date()).getTime()
      // const res = await articlesListAPI({
      //   channel_id: this.id,
      //   timestamp: this.theTime
      // })

      // this.list = [...this.list, ...res.data.data.results]
      // this.theTime = res.data.data.pre_timestamp
      // this.isLoading = false
      this.sendRequest()
    },
    async disLikeFn (id) {
      try {
        await dislikeNew(id)
        Notify({ type: 'success', message: '反馈成功' })
      } catch (err) {
        console.dir(err)
      }
    },
    async reportFn (id, type) {
      await reportArticleAPI(id, type)
      Notify({ type: 'success', message: '举报成功' })
    }
  }
}
</script>

<style>
</style>
